package com.itheima.servlet;

import com.itheima.domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/role/*")
public class RoleServlet extends BaseServlet {

    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //模拟一个对象数据
        User user =new User();
        user.setId(1);
        user.setUsername("ddiaochan");
        User user1 =new User();
        user1.setId(2);
        user1.setUsername("diaochan1");
        User user2 =new User();
        user2.setId(3);
        user2.setUsername("diaochan2");
        //创建一个List
        List<User> list =new ArrayList<>();
        list.add(user);
        list.add(user1);
        list.add(user2);
        //将List对象存入Request作用域
        req.setAttribute("userList",list);
        //服务器端跳转
        req.getRequestDispatcher("/page/demo3.jsp").forward(req,resp);
    }


    private void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //模拟一个对象数据
      User user =new User();
      user.setId(1);
      user.setUsername("diaochan");
      //将user对象存入Request作用域
      req.setAttribute("u",user);
     //服务器端跳转
      req.getRequestDispatcher("/page/demo3.jsp").forward(req,resp);
    }

    private void saveRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("role save");
        String username ="admin";
        //将username存入作用域中
        req.setAttribute("name",username);
        req.getRequestDispatcher("/page/demo3.jsp").forward(req,resp);
    }
}
